package org.example.system.service.impl;

import org.example.system.entity.SysResource;
import org.example.system.entity.SysRole;
import org.example.system.service.ISysResourceService;
import org.example.system.service.ISysRoleService;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @version 1.0.0
 * @className: SysPermissionService
 * @description:
 * @author: LiJunYi
 * @create: 2022/7/26 12:36
 */
@Component
public class SysPermissionService
{
    private final ISysRoleService roleService;
    private final ISysResourceService resourceService;

    /**
     * 构造器注入
     *
     * @param roleService     角色服务
     * @param resourceService 资源服务
     */
    public SysPermissionService(ISysRoleService roleService, ISysResourceService resourceService) {
        this.roleService = roleService;
        this.resourceService = resourceService;
    }

    /**
     * 获取角色通过用户id
     *
     * @param id id
     * @return {@link Set}<{@link String}>
     */
    public Set<String> getRolesByUserId(Long id)
    {
        List<SysRole> roles =  roleService.getRolesByUserId(id);
        return roles.stream().map(SysRole::getRoleKey).collect(Collectors.toSet());
    }

    /**
     * 获取菜单许可
     *
     * @param id id
     * @return {@link Set}<{@link String}>
     */
    public Set<String> getMenuPermissionByUserId(Long id)
    {
        List<SysResource> menus = resourceService.getMenuPermissionByUserId(id);
        return menus.stream().map(SysResource::getPermission).collect(Collectors.toSet());
    }
}
